#coding=utf-8
from bson import ObjectId
import datetime
from web import storage

__author__ = "jy@cjlu.edu.cn"
from iLibM.dalbase import DalBase

class dal_todos(DalBase):
    """
    用于处理与数据表d_bankinfo进行持久化操作的相关功能
    调用iLibp的基础函数
    """
    def __init__(self):
        DalBase.__init__(self,"todos","id",["title","content","createuser","createtime","targetuser","targetusertruename","targetuseremail","limitdate","logs","status"])

class bll_todos(dal_todos):
    """
    用于处理与业务有关的操作，用于定义辅助WEB的一些功能，尽可能不在WEB前端直接调用dal函数或web.py的持久层函数
    需要调用时请先在此定义入口与出口参数
    """
    def __init__(self):
        dal_todos.__init__(self)

    def getTodo(self,id):
        #获取一条todo
        return self.GetRowById(id)

    def getUserUnclearTodo(self,pageindex,pagesize,username):
        #获取到某人未清掉的todo
        filter={"username":username,"status":"unclear"}
        return  self.GetPagedRowsByFilterDict(pageindex,pagesize,filter).sort({"createtime":-1} )

    def getUserClearedTodo(self,pageindex,pagesize,username):
        filter={"username":username,"status":"clear"}
        return  self.GetPagedRowsByFilterDict(pageindex,pagesize,filter).sort({"createtime":-1} )

    def clearTodo(self,obj):
        """
              清理某一条todo，意味着完成
        """
        if obj.id!=0:
            self.UpdateColValue(obj.id,"status","clear")
            return True
        else:
            return False

    def getUnclear(self):
        filter={"status":"unclear"}
        sort={"createtime":-1}#创建时间倒序
        self.GetRowsByFilterDict(filter).sort()
		
if __name__=="__main__":
    obj=bll_todos()
    recs=obj.GetAll()
    for r in recs:
        print r
    r,p,i=obj.GetPagedRowsByFilterDict(1,2,{})
    for aa in r:
        print aa